package com.dzxcqmxm.Dao.impl;

import com.dzxcqmxm.Dao.Dao;
import com.dzxcqmxm.Dao.PhotoDao;
import com.dzxcqmxm.JB.Album;
import com.dzxcqmxm.JB.Photo;
import com.dzxcqmxm.JB.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @author Payboi
 * @create 2022/6/4-12:42
 * @FileName PhotoDaoImpl
 * @Description
 */
public class PhotoDaoImpl implements PhotoDao {
    @Override
    public void addPhoto(Photo photo) throws Exception {
        //数据库插入数据
        String sql4 = "INSERT INTO `photo` VALUES (?,?,?,?,?,?,?);";
        try {
            //连接数据源
            Connection connection = getConnection();
            //得到语句对象
            PreparedStatement pstm = connection.prepareStatement(sql4);
            //设置插入值
            pstm.setString(1,photo.getAlbumName());
            pstm.setString(2,photo.getAlbumUrl());
            pstm.setString(3,photo.getUserName());
            pstm.setString(4,photo.getPhotoName());
            pstm.setString(5,photo.getPhotoUrl());
            pstm.setString(6,photo.getPhotoBaoMi());
            pstm.setString(7,photo.getBenDiPhotoUrl());
            //执行插入语句
            pstm.executeUpdate();

            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("插入数据失败！");
        }

    }

    @Override
    public ArrayList<Photo> photoFindByNameAndPhotoBaoMi(String PhotoName,String PhotoBaoMi) throws Exception {

        String sql = "select * From photo where PhotoName =? And PhotoBaoMi=?";
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();
        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,PhotoName);
        pstm.setString(2,PhotoBaoMi);

        ResultSet rs = pstm.executeQuery();

        while (rs.next()){
            Photo photo = new Photo();
            photo.setAlbumName(rs.getString("AlbumName"));
            photo.setAlbumUrl(rs.getString("AlbumUrl"));
            photo.setUserName(rs.getString("UserName"));
            photo.setPhotoName(rs.getString("PhotoName"));
            photo.setPhotoUrl(rs.getString("PhotoUrl"));
            photo.setPhotoBaoMi(rs.getString("PhotoBaoMi"));
            photo.setBenDiPhotoUrl(rs.getString("BenDiPhotoUrl"));


            photoArrayList.add(photo);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return photoArrayList;
    }

    @Override
    public ArrayList<Photo> findAllphoto() throws Exception {
        String sql4 = "select * from photo";
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();
        try {
            Connection connection = getConnection();
            PreparedStatement preparedStatement2 = connection.prepareStatement(sql4);
            //执行查询语句（赋值给结果集）
            ResultSet rs = preparedStatement2.executeQuery();
            while (rs.next()){
                Photo photo = new Photo();
                photo.setAlbumName(rs.getString("AlbumName"));
                photo.setAlbumUrl(rs.getString("AlbumUrl"));
                photo.setUserName(rs.getString("UserName"));
                photo.setPhotoName(rs.getString("PhotoName"));
                photo.setPhotoUrl(rs.getString("PhotoUrl"));
                photo.setPhotoBaoMi(rs.getString("PhotoBaoMi"));
                photo.setBenDiPhotoUrl(rs.getString("BenDiPhotoUrl"));
                photoArrayList.add(photo);
            }
            //关闭连接，防止卡住！
            Dao.close(rs,preparedStatement2,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("查询所有失败");
            return null;

        }
        return photoArrayList;
    }

    @Override
    public void delPhoto(String PhotoName) throws Exception {
        String sql = "DELETE FROM photo WHERE PhotoName =?";

        try {
            Connection connection = getConnection();
            PreparedStatement pstm = connection.prepareStatement(sql);

            //设置要删除的语句，这个获取的id是就是上面那个where id啦
            pstm.setString(1,PhotoName);
            //执行了删除语句
            pstm.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,pstm,connection);

        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("删除失败！！");

        }
    }

    /**
     * 修改照片是否保密
     * @param photo
     * @return
     * @throws Exception
     */
    @Override
    public Photo updatePhoto(Photo photo) throws Exception {
        String sql2 = "update photo set PhotoBaoMi=? where PhotoName=?";
        Connection connection = getConnection();

        try {
            PreparedStatement preparedStatement4 = connection.prepareStatement(sql2);//得到语句对象
            preparedStatement4.setString(1,photo.getPhotoBaoMi());
            preparedStatement4.setString(2,photo.getPhotoName());

            preparedStatement4.executeUpdate();
            //关闭连接，防止卡住！
            Dao.close(null,preparedStatement4,connection);

        } catch (SQLException e) {
            System.out.println("更新数据失败!!!!");
            e.printStackTrace();
        }

        return photo;
    }

    @Override
    public ArrayList<Photo> findPhotoByUserNameAndAlbumUrl(String UserName, String AlbumUrl) throws Exception {

        String sql = "select * From photo where userName =? And AlbumUrl=?";
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();
        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,UserName);
        pstm.setString(2,AlbumUrl);

        ResultSet rs = pstm.executeQuery();

        while (rs.next()){
            Photo photo = new Photo();

            photo.setAlbumName(rs.getString("AlbumName"));
            photo.setAlbumUrl(rs.getString("AlbumUrl"));
            photo.setUserName(rs.getString("UserName"));
            photo.setPhotoName(rs.getString("PhotoName"));
            photo.setPhotoUrl(rs.getString("PhotoUrl"));
            photo.setPhotoBaoMi(rs.getString("PhotoBaoMi"));
            photo.setBenDiPhotoUrl(rs.getString("BenDiPhotoUrl"));

            photoArrayList.add(photo);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return photoArrayList;
    }

    @Override
    public ArrayList<Photo> photoFindbyAlbumUrl(String AlbumUrl) throws Exception {

        String sql = "select * from photo WHERE AlbumUrl=?";
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();

        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,AlbumUrl);
        ResultSet rs = pstm.executeQuery();

        if(rs.next()){

            Photo photo = new Photo();
            photo.setAlbumName(rs.getString("AlbumName"));
            photo.setAlbumUrl(rs.getString("AlbumUrl"));
            photo.setUserName(rs.getString("UserName"));
            photo.setPhotoName(rs.getString("PhotoName"));
            photo.setPhotoUrl(rs.getString("PhotoUrl"));
            photo.setPhotoBaoMi(rs.getString("PhotoBaoMi"));
            photo.setBenDiPhotoUrl(rs.getString("BenDiPhotoUrl"));

            photoArrayList.add(photo);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return photoArrayList;
    }

    @Override
    public ArrayList<Photo> photoFindbyPhotoName(String PhotoName) throws Exception {

        String sql = "select * from photo WHERE PhotoName=?";
        ArrayList<Photo> photoArrayList = new ArrayList<Photo>();

        //连接数据源
        Connection connection = getConnection();
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,PhotoName);
        ResultSet rs = pstm.executeQuery();

        if(rs.next()){

            Photo photo = new Photo();
            photo.setAlbumName(rs.getString("AlbumName"));
            photo.setAlbumUrl(rs.getString("AlbumUrl"));
            photo.setUserName(rs.getString("UserName"));
            photo.setPhotoName(rs.getString("PhotoName"));
            photo.setPhotoUrl(rs.getString("PhotoUrl"));
            photo.setPhotoBaoMi(rs.getString("PhotoBaoMi"));
            photo.setBenDiPhotoUrl(rs.getString("BenDiPhotoUrl"));

            photoArrayList.add(photo);
        }
        //关闭连接
        Dao.close(rs,pstm,connection);
        return photoArrayList;
    }
}
